package com.itheima.多线程.基础;

public class 同步方法synchronized implements Runnable {
    private static int count = 100;

    @Override
    public void run() {
        while (true) {
            if ("窗口一".equals(Thread.currentThread().getName())) {
                boolean result = synchnorized();
                if (result) {
                    break;
                }
            }
            if ("窗口二".equals(Thread.currentThread().getName())) {
                synchronized (this) {
                    if (count == 0) {
                        break;
                    } else {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            //e.printStackTrace();
                        }
                        count--;
                        System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + count);
                    }
                }
            }
        }
    }

    private synchronized boolean synchnorized() {
        if (count == 0) {
            return true;
        } else {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
            count--;
            System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + count);
            return false;
        }
    }

    private static synchronized boolean ssynchronized() {
        if (count == 0) {
            return true;
        } else {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
            count--;
            System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + count);
            return false;
        }
    }
}


class Testt {
    public static void main(String[] args) {
        同步方法synchronized ss = new 同步方法synchronized();
        Thread th1 = new Thread(ss, "窗口一");
        Thread th2 = new Thread(ss, "窗口二");
        //th2.start();
        //th1.start();
        Ssynch s=new Ssynch();
        Thread th3=new Thread(s,"窗口一");
        Thread th4=new Thread(s,"窗口二");
        th3.start();
        th4.start();

    }
}


class Ssynch implements Runnable {
    private static int count = 100;

    @Override
    public void run() {
        while (true) {
            if ("窗口一".equals(Thread.currentThread().getName())) {
                boolean result = synchnorized();
                if (result) {
                    break;
                }
            }
            if ("窗口二".equals(Thread.currentThread().getName())) {
                synchronized (Ssynch.class) {
                    if (count == 0) {
                        break;
                    } else {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            //e.printStackTrace();
                        }
                        count--;
                        System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + count);
                    }
                }
            }
        }
    }

    private static synchronized boolean synchnorized() {
        if (count == 0) {
            return true;
        } else {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
            count--;
            System.out.println(Thread.currentThread().getName() + "在卖票,还剩" + count);
            return false;
        }
    }
}